﻿using CefSharp;
using CefSharp.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class frmMain : Form
    {
        public static string url = "https://www.baidu.com";//设置进入网页
        public ChromiumWebBrowser browser = new ChromiumWebBrowser();//初始化cefsharp浏览器
        public frmMain()
        {
            InitializeComponent();
            browser.Dock = DockStyle.Fill;//设置填充满窗体
            panel1.Controls.Add(browser);//把cefsharp加入到窗体中
            browser.RequestHandler = new WinFormsRequestHandler();//request请求的具体实现

            IniFile.SaveFormSet(this);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //CefSharp.Cef.Shutdown();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //注入本机bin\x86\debug下的文件
            browser.ExecuteScriptAsync(File.ReadAllText(@"JavaScript2.js"));
            //也可以注入网络文件

            // 调用网页上已有js方法，等待js 方法执行完后，获取返回值
            Task<CefSharp.JavascriptResponse> t = browser.GetBrowser().MainFrame.EvaluateScriptAsync("testGetData()");
            t.Wait();
            // t.Result 是 CefSharp.JavascriptResponse 对象
            // t.Result.Result 是一个 object 对象
            if (t.Result.Result != null)
            {
                MessageBox.Show(t.Result.Result.ToString());
            }

            //注入js代码。当然也可以是jquery代码。如果页面没有jq库，要先注入jq库
            //下面演示给 当前地址包含login的， 界面上div class=develop 的第一个input赋值aaaa
            var curUrl = browser.Address.ToLower();
            if (curUrl.Contains("login".ToLower()))
            {
                string script = "";
                script += "(function btn() {";
                script += " var obj = document.getElementsByClassName('develop');";
                script += " if (obj.length > 0)";
                script += " {";
                script += "  obj[0].getElementsByTagName('input')[0].focus();";
                script += "  obj[0].getElementsByTagName('input')[0].value ='aaaa';";
                script += " }";
                script += " })()";
                //注入执行js
                browser.ExecuteScriptAsync(script);
            }

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            browser.Load(txUrl.Text);
        }

        private void 保存网站_CheckedChanged(object sender, EventArgs e)
        {
            WinFormResourceRequestHandler.EnableSaveData = 保存网站.Checked;
            WinFormResourceRequestHandler.SaveDataDir = txDir.Text;
        }

        private void txDir_TextChanged(object sender, EventArgs e)
        {
            WinFormResourceRequestHandler.EnableSaveData = 保存网站.Checked;
            WinFormResourceRequestHandler.SaveDataDir = txDir.Text;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            System.Environment.Exit(0);
        }
    }
}
